﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using System.Xml.Serialization;
using System.Data.SqlClient;
using System.Reflection;
using System.Collections;

namespace EntityMap.Meta
{   
    /// <summary>
    /// This class is used for manage parameters.
    /// </summary>
    public class ParameterMap : Map
    {
        [XmlArray( "params" )]
        [XmlArrayItem( typeof( Param ), ElementName = "param" )]
        public Collection<Param> Params = new Collection<Param>();

        public ParameterMap() {}

        public SqlParameter[] GetSqlParameters( object obj )
        {
            Type t = obj.GetType();

            ArrayList alist = new ArrayList();

            foreach( Param p in Params )
            {
                object newValue = t.GetProperty( p.Property ).GetValue( obj, null );

                alist.Add( new SqlParameter( p.Name, newValue ) );
            }
            return alist.ToArray( typeof( SqlParameter) ) as SqlParameter[];
        }
    }
}
